package uestc.bitman.edxapp.downloading;

import uestc.bitman.edxapp.EdxApp;
import uestc.bitman.edxapp.R;
import uestc.bitman.edxapp.mainui.EdxAppFragment;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * @category UI fragment of downloader
 */
public class DownloadFragment extends EdxAppFragment {

	private ListView mListView;

	private DownloadingTaskAdapter mDownloadingAdapter;

	private DownloadingListItem mDownloadingItem = null;

	public DownloadFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.download_fragment, container,
				false);
		mListView = (ListView) view
				.findViewById(R.id.list_coursest_downloading);
		mDownloadingAdapter = EdxApp.getDownloadingTaskAdapter();

		mListView.setAdapter(mDownloadingAdapter);

		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				mDownloadingAdapter.getItem(position).toggle();
			}
		});

		mListView
				.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
					@Override
					public void onCreateContextMenu(ContextMenu menu, View v,
							ContextMenuInfo menuInfo) {

						mDownloadingItem = (DownloadingListItem) v.getTag();
						menu.setHeaderTitle(R.string.title_select_option);
						menu.add(0, 0, 0, getString(R.string.label_delete));
					}
				});

		return view;
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == 0) {
			if (mDownloadingItem != null) {
				mDownloadingAdapter.remove(mDownloadingItem);
				mDownloadingItem = null;
			}
		}
		return false;
	}


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        
    }
}
